package cn.xxc.org.service.impl;

import cn.xxc.basic.service.impl.BaseServiceImpl;
import cn.xxc.basic.util.Md5Utils;
import cn.xxc.basic.util.StrUtils;
import cn.xxc.org.domain.Employee;
import cn.xxc.org.mapper.EmployeeMapper;
import cn.xxc.org.service.EmployeeService;
import cn.xxc.user.domain.Logininfo;
import cn.xxc.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void save(Employee employee) {
        //设置默认密码为1
        employee.setSalt(StrUtils.getComplexRandomString(32));
        employee.setPassword(Md5Utils.encrypByMd5("1"+employee.getState()));

        //添加登录信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        if (employee.getShop_id() == null) {
            logininfo.setType(1);
        }else {
            logininfo.setType(0);
        }
        if (employee.getState() == 1) {
            logininfo.setDisable(true);
        }else {
            logininfo.setDisable(false);
        }
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        //添加t_employee:添加中间表信息：t_employee_role
        employeeMapper.addtEmployeeRole(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        String password = employee.getPassword();
        employee.setPassword(Md5Utils.encrypByMd5(password+employee.getSalt()));
        super.update(employee);
        Logininfo logininfo = logininfoMapper.loadByid(employee.getLogininfo_id());
        Long id=logininfo.getId();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setId(id);
        if (employee.getShop_id() == null) {
            logininfo.setType(1);
        }else {
            logininfo.setType(0);
        }
        if (employee.getState() == 1) {
            logininfo.setDisable(true);
        }else {
            logininfo.setDisable(false);
        }
        logininfoMapper.update(logininfo);

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());

        //添加t_employee:添加中间表信息：t_employee_role
        employeeMapper.addtEmployeeRole(employee);

    }

    @Override
    @Transactional
    public void del(Long id) {

        Employee employee = employeeMapper.loadByid(id);
        //删除登录信息表数据
        logininfoMapper.del(employee.getLogininfo_id());

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);

        //删除employee表数据
        super.del(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> loadByshopId(Long shopId) {
        return employeeMapper.loadByshopId(shopId);
    }
}
